/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import core.DriverManager;
import core.OrderManager;
import core.ScreenEngine;
import datamodel.Order;
import view.commands.BackCommand;
import view.commands.CenterCommand;

/**
 *
 * @author Kvest
 */
public class OrderInformationForm extends Form 
{
    private static final int FUNCTIONS_COMMAND_ID = 1;
    private static final int EMPTY_COMMAND_ID = 2;
    
    private TextArea text;
    private Command functionsCommand;
    private BackCommand backCommand;
    private CenterCommand centerCommand;
    private Command emptyCommand;
    
    private TimeFunctionForm timeFunctionForm;
    private int orderID;
    
    public OrderInformationForm()
    {
        super("order");
        
        timeFunctionForm = new TimeFunctionForm();
        
        init();
    }
    
    private void init()
    {
        setLayout(new BorderLayout());
        setScrollable(false);
        
        text = new TextArea("");
        text.setEditable(false);
        text.setGrowByContent(true);
        addComponent(BorderLayout.NORTH, text);

        functionsCommand = new Command("time", FUNCTIONS_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                //Отображаем форму выбора времени, если водитель не в черном списке
                if (!DriverManager.getManager().isInBlackList())
                {
                    ScreenEngine.getEngine().push(timeFunctionForm);
                }    
                evt.consume();
            }
        };
        
        backCommand = new BackCommand();
        centerCommand = new CenterCommand(functionsCommand);
        emptyCommand = new Command(" ", EMPTY_COMMAND_ID);
    }
    
    public void updateOrder(Order order)
    {
        text.setText(order.getFullText());
    }
    
    public void setOrder(int orderID)
    {
        this.orderID = orderID;
        timeFunctionForm.setOrderID(orderID);
        
        removeAllCommands();
        
        Order order = OrderManager.getManager().getOrder(orderID);
        if (order != null)
        {
            text.setText(order.getFullText());
            if (!Display.getInstance().isTouchScreenDevice())
            {
                addCommand(centerCommand);
            }    
            addCommand(functionsCommand);
        }
        else
        {
            text.setText((new Label("order_not_found")).getText());
            addCommand(emptyCommand);
        }
        
        addCommand(backCommand);
    }

    public int getOrderID() 
    {
        return orderID;
    }
}
